$(function () {
    var user = {}, vcode = '', _thisPage = null, html = [], user = null, passType = "login", _parms = {}, requestUrl = '';

    var editpassPage = function () {
        this.init();
    };

    editpassPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (user) {
                _parms = getPageParams("validatePhone_html");
                passType = _parms.type;
                vcode = _parms.vcode;
                switch (passType) {
                    case "login":
                        $("#updateType").html("修改登录密码");
                        break;
                    case "pay":
                        $("#updateType").html("修改支付密码");
                        break;
                    default:
                        break;
                }
            } else {
                redirect("login.html");
                return false;
            }
        },
        bindEvent: function () {
            //点击回退
            $("#back").on("tap", function () {
                back();
            });

            //点击清除
            $("#clear").on("tap", function () {
                $("#nPassword,#confirmNpassword").val("");
            });

            //点击提交
            $("#confirm").on("tap", function () {
                if ("" == $("#nPassword").val()) {
                    $.alert("请填写新密码");
                    return false;
                }
                if ("" == $("#confirmNpassword").val()) {
                    $.alert("请再次填写新原密码");
                    return false;
                }
                if (!isPasswd($("#nPassword").val())) {
                    $.alert("新密码的格式不正确");
                    return false;
                }
                if (!isPasswd($("#confirmNpassword").val())) {
                    $.alert("确认新密码的格式不正确");
                    return false;
                }
                requestUrl = CONFIG_MANAGER.RequstUrl;
                switch (passType) {
                    case "login":
                        requestUrl += "v1/member/safety/loginpasswd/";
                        break;
                    case "pay":
                        requestUrl += "v1/member/safety/paypasswd/";
                        break;
                    default:
                        break;
                }
                var postData = {"id": user.id, "vcode": vcode, "passwd": $("#confirmNpassword").val()};
                $.ajax({
                    type: "PUT",
                    url: requestUrl,
                    async: true,
                    data: postData,
                    headers: {"token": user.token}
                }).done(function (data) {
                    $.alert("修改成功", function () {
                        redirect("account.html");
                        return false;
                    });
                }).always(function () {
                }).fail(function (data) {
                    var infor = JSON.parse(data);
                    $.toast(infor.Message);
                });

            });

        },
    }

    _thisPage = new editpassPage();

})
